<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class PaymentConfig extends AdminController{
    public $_validation = null;
    
    function __construct(){
        parent::__construct();
        $this->load->model('MPaymentConfig');
        $this->_validation = Array(
            array('field'=>'code', 'label'=>'标识', 'rules'=>'trim|required|max_length[32]|callback__checkCode')
            ,array('field'=>'gateway', 'label'=>'网关', 'rules'=>'trim|required|max_length[64]')
            ,array('field'=>'view', 'label'=>'前端显示', 'rules'=>'trim|max_length[64]')
            ,array('field'=>'name', 'label'=>'名称', 'rules'=>'trim|required|max_length[128]')
            ,array('field'=>'description', 'label'=>'温馨提示', 'rules'=>'trim')
            ,array('field'=>'sort', 'label'=>'排序', 'rules'=>'integer|max_length[11]|callback__checkSort')
            ,array('field'=>'configs', 'label'=>'限额', 'rules'=>'trim')
            ,array('field'=>'status', 'label'=>'状态', 'rules'=>'integer')
            ,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
            );
    }
    
    function _checkCode($code){
        $where = Array('code'=>$code);
        if(isset($this->editObj) && $this->editObj){
            $where['id <>'] = $this->editObj->id;
        }
        if($this->MPaymentConfig->getAll($where)){
            $this->form_validation->set_message('_checkCode', '已经存在相同标识的三方通道！');
            return false;
        }
        return true;
    }
    function _checkSort($sort){
        $where = Array('sort'=>$sort);
        if(isset($this->editObj) && $this->editObj){
            $where['id <>'] = $this->editObj->id;
        }
        if($this->MPaymentConfig->getAll($where)){
            $this->form_validation->set_message('_checkSort', '已经存在相同排序的三方通道！');
            return false;
        }
        return true;
    }
    
    function index($game_tag=null){
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $this->formfilter->addFilter('name', 'like');
        $this->formfilter->addFilter('code','where');
        $this->formfilter->addFilter('gateway','where');
        $this->formfilter->addFilter('sort','where');
        $this->formfilter->addFilter('status','where');
        $limit = $this->pagination($this->MPaymentConfig->getCount());
        $list = $this->MPaymentConfig->getList($limit,filterValue('sort_type'));
        $this->assign('list', $list);
        $this->layout();
    }
    
    function add(){
        $obj = $this->MPaymentConfig->createVo();
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->_validation);
        if($this->form_validation->run()==TRUE){
            $this->load->helper('populate');
            $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
            if($this->MPaymentConfig->add($newObj)){
                successAndRedirect(l('add_success'));
            }else{
                errorAndRedirect(l('add_faild'));
            }
        }
        $this->assign('obj', $obj);
        $this->layout();
    }
    
    function edit($id){
        if(!is_numeric($id)){
            show_error(l('id_not_null'));
        }
        $obj = $this->MPaymentConfig->getOne(Array('id'=>$id));
        if(!$obj){
            errorAndRedirect('未找到定义！');
        }
        $this->editObj = $obj;
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->_validation);
        if($this->form_validation->run()==TRUE){
            $this->load->helper('populate');
            $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
            if($this->MPaymentConfig->save($newObj)){
                successAndRedirect(l('edit_success'));
            }else{
                errorAndRedirect(l('data_fail'));
            }
        }
        $this->assign('obj', $obj);
        $this->layout();
    }
    function delete($id, $update_time){
        if(!is_numeric($id) || !is_numeric($update_time)){
            show_error(l('id_not_null'));
        }
        if($this->MPaymentConfig->delete($id,$update_time)){
            successAndRedirect(l('delete_success'));
        }else{
            errorAndRedirect(l('data_fail'));
        }
    }
    
    
}